import {
  getCardDetails,
  toMailList
} from "../../apis/card";
const app = getApp();

Page({
  data: {
    cardInfo: {},
    isRequest: false
  },

  onLoad(options) {
    this.setData({
      ...options
    });
    this.getCardDetails(options.cardId);
  },

  getCardDetails(id) {
    getCardDetails(id).then(res => {
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        if (!new RegExp('https://').test(data.avatar) && !new RegExp('http://').test(data.avatar)) data.avatar = app.globalData.url + data.avatar;
        this.setData({
          cardInfo: data
        });
      };
    });
  },

  // 添加
  addCard() {
    if (this.data.isRequest) {
      return
    };
    this.setData({
      isRequest: true
    });
    toMailList(this.data.cardId).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          mask: true,
          duration: 1500
        });
        this.setData({
          isRequest: false
        });
      };
    });
  },
})